package com.example.lemis.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.lemis.common.utils.SessionUtils;
import com.example.lemis.common.exception.NotLoginException;
import com.example.lemis.model.dto.PageDTO;
import com.example.lemis.model.dto.UserLoginRequest;
import com.example.lemis.model.dto.UserRegisterRequest;
import com.example.lemis.model.dto.passwordChangeDTO;
import com.example.lemis.model.dto.resetPasswordDTO;
import com.example.lemis.model.vo.UserInfoVO;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import com.example.lemis.common.JsonResponse;
import com.example.lemis.service.IUserService;
import com.example.lemis.model.domain.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2025-06-24
 * @version v1.0
 */
@RestController
@AllArgsConstructor
@RequestMapping("/api/user")
public class UserController {

    private final Logger logger = LoggerFactory.getLogger( UserController.class );

    private final IUserService userService;

    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse<User> getById(@PathVariable("id") Long id)throws Exception {
        User user = userService.getById(id);
        return JsonResponse.success(user);
    }

    @PostMapping("/login")
    public JsonResponse<UserInfoVO> login(@Validated @RequestBody UserLoginRequest user) throws Exception {
        UserInfoVO userInfoVO = userService.login(user);
        if (userInfoVO == null) {
            throw new NotLoginException("用户名或密码错误");
        }
        return JsonResponse.success(userInfoVO);
    }

    /**
     *描述：修改密码
     */
    @PostMapping("/password")
    public JsonResponse<UserInfoVO> password_change(@RequestBody passwordChangeDTO pcDTO) throws Exception {
        User nowUser = SessionUtils.getCurrentUserInfo();
        if (nowUser == null) {
            return JsonResponse.failure("未登录");
        }
        UserInfoVO user = userService.password_change(pcDTO);
        if (user == null) {
            return JsonResponse.failure("未找到该用户");
        }
        return JsonResponse.success(user);
    }

    /**
     * 描述：用户管理
     */
    @RequestMapping("/modify")
    public  JsonResponse<User> infoModify(@RequestBody User user)throws Exception{
        User u = userService.infoModify(user);
        return JsonResponse.success(u);
    }

    /**
     * 描述：重置密码
     */
    @RequestMapping("/reset")
    public JsonResponse<UserInfoVO> resetPassword(@RequestBody resetPasswordDTO rpdto)throws Exception {
        User nowUser = SessionUtils.getCurrentUserInfo();
        if (nowUser == null) {
            return JsonResponse.failure("用户未登录");
        }
        if (!rpdto.getName().equals(nowUser.getName())) {
            return JsonResponse.failure("用户名错误");
        }
        if (rpdto.getNewPassword() == null) {
            return JsonResponse.failure("密码格式错误或不能为空");
        }
        else {
            nowUser.setName(rpdto.getName());
            SessionUtils.saveCurrentUserInfo(nowUser);
        }
        userService.resetPassword(rpdto);
        UserInfoVO userInfoVO = new UserInfoVO();
        BeanUtils.copyProperties(nowUser, userInfoVO);
        return JsonResponse.success(userInfoVO);

    }

    @GetMapping("/list")
    public JsonResponse<?> searchUserList(PageDTO pageDTO, String name) throws Exception {
        Page<User> page = userService.searchUserList(pageDTO, name);
        return JsonResponse.success(page);
    }

    @PostMapping("/delete")
    public JsonResponse<?> deleteUser(@RequestBody User user) throws Exception {
        UserInfoVO u = userService.deleteUser(user);
        if (u == null) {
            return JsonResponse.message(true, "删除成功");
        }
        else{
            return JsonResponse.failure("删除失败");
        }
    }

    @GetMapping("/auth")
    public JsonResponse<UserInfoVO> getCurrentUser() throws Exception {
        UserInfoVO userInfo= userService.auth();
        return JsonResponse.success(userInfo, "已登录");
    }

    @PostMapping("/register")
    public JsonResponse<User> register(@Validated @RequestBody UserRegisterRequest user) throws Exception {
        System.out.println("666666666666666666666666666666666666666666666666666666");
        System.out.println(user);
        User userInfoVO = userService.register(user);
        return JsonResponse.success(userInfoVO);
    }
    @PostMapping("/add")
    public JsonResponse<?> addUser(@RequestBody User user) throws Exception {
        User u = userService.addUser(user);
        return JsonResponse.success(u);
    }
}

